package bg.unisofia.fmi.tis.service;

import java.util.HashMap;
import java.util.Map;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;

import bg.unisofia.fmi.tis.model.Thesis;

public class JBPMUtil {
	
	private static ProcessEngine processEngine;
	
	public static void init() {
		// Build jBPM services
		processEngine = new Configuration().buildProcessEngine();
		
		// Deploy process definition
		RepositoryService repositoryService = processEngine.getRepositoryService();
		repositoryService.createDeployment().addResourceFromClasspath("thesis_workflow.jpdl.xml").deploy();
	}
	
	public static void startProcess(Thesis thesis) {
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("thesisId", thesis.getId());
		
		ExecutionService executionService = processEngine.getExecutionService();
		ProcessInstance process = 
			executionService.startProcessInstanceByKey(
					"thesis_workflow",
					variables,
					String.valueOf(thesis.getId()));
		
		System.out.println(">> PROCESS STATE: " + process.getState());
	}
	
	public static void continueProcess(Thesis thesis) {
		ExecutionService executionService = processEngine.getExecutionService();
		ProcessInstance process = 
			executionService.signalExecutionById("thesis_workflow." + String.valueOf(thesis.getId()));
		
		System.out.println(">> PROCESS STATE: " + process.getState());
	}
}
